#include "history.h"

/** Do historie prida dalsi vysledek.
 * \param f pouzite frekvencni pasmo
 * \param al_dt nastaveni algoritmu
 * \param ar_dt nastaveni mapy
 * \return doba vypoctu algoritmu
 */
double History::push(int f, std::string al_dt, std::string ar_dt) {
	double time = timer.lap();
	results.push_back(HistoryField(f, time, al_dt, ar_dt));
	return time;
}

/** Vypise do daneho vystupniho streamu tabulku s vysledky.
 * \param o vystupni stream
 * \param h historie vysledku
 * \return vystupni stream
 */
std::ostream & operator <<(std::ostream & o, const History & h) {
	// vytisknout zacatek tabulky
	o << std::endl << "--- RESULTS: " << std::string(64, '-') << std::endl << "| C | ALGORITMUS                  | MAPA              | FREQ | CAS    | " << std::endl;
	int j = 0;
	for (std::vector<HistoryField>::const_iterator i = h.results.begin(); i != h.results.end(); ++i) {
		o << "|" << std::setw(3) << ++j << *i;
	}
	o << std::string(77, '-') << std::endl;
	// konec tabulky
	return o;
}

/** Doplni retezec zprava mezerami, aby mel pozadovanou delku.
 * \param s retezec
 * \param length pozadovana delka
 * \return zarovnany retezec
 */
std::string string_align(std::string s, int length) {
	s = s.substr(0, length);
	s += std::string(length - s.length(),' ');
	return s;
}

/** Vypise jedno policko historie vysledku do vystupniho streamu.
 * \param o vystupni stream
 * \param h vysledek jednoho algoritmu
 * \return vystupni stream
 */
std::ostream & operator <<(std::ostream & o, const HistoryField & h) {
	o	<< "| " << string_align(h.algorithm_detail, 28) << "| " << string_align(h.area_detail, 18)
		<< "|" << std::setw(6) << h.frequencies << "| " << std::fixed << std::setprecision(2) << h.time << " s | " << std::endl;
	return o;
}
